package com.neusoft.elm.dao.impl;

import com.neusoft.elm.dao.CartDao;
import com.neusoft.elm.po.Business;
import com.neusoft.elm.po.Cart;
import com.neusoft.elm.po.Food;
import com.neusoft.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class CartDaoImpl implements CartDao {
    Connection conn=null;
    PreparedStatement prep=null;
    ResultSet rs=null;
    @Override
    public List<Cart> listCart(Cart cart) throws Exception {
        try {
            List<Cart> list=new ArrayList<>();
            conn= DBUtil.getConnection();
            List params=new ArrayList();
            StringBuilder sb=new StringBuilder();
            sb.append("select c.*,f.*,b.* ");
            sb.append("from cart c left join food f ");
            sb.append("on c.foodId=f.foodId ");
            sb.append("left join business b ");
            sb.append("on c.businessId=b.businessId ");
            sb.append("where 1=1 ");
            if (cart.getFoodId()!=null){
                sb.append("and c.foodId=? ");
                params.add(cart.getFoodId());
            }
            if (cart.getBusinessId()!=null){
                sb.append("and c.businessId=? ");
                params.add(cart.getBusinessId());
            }
            if (cart.getUserId()!=null){
                sb.append("and c.userId=? ");
                params.add(cart.getUserId());
            }
            System.out.println(sb.toString());
            prep=conn.prepareStatement(sb.toString());
            for (int i = 0; i <params.size() ; i++) {
                prep.setObject(i+1,params.get(i));
            }
            rs=prep.executeQuery();
            while (rs.next()){
                Cart c=new Cart();
                c.setCartId(rs.getInt("cartId"));
                c.setBusinessId(rs.getInt("businessId"));
                c.setFoodId(rs.getInt("foodId"));
                c.setQuantity(rs.getInt("quantity"));
                c.setUserId(rs.getString("userId"));
                Food food=new Food();
                food.setBusinessId(rs.getInt("businessId"));
                food.setFoodExplain(rs.getString("foodExplain"));
                food.setFoodId(rs.getInt("foodId"));
                food.setFoodImg(rs.getString("foodImg"));
                food.setFoodName(rs.getString("foodName"));
                food.setFoodPrice(rs.getDouble("foodPrice"));
                food.setRemarks(rs.getString("remarks"));
                c.setFood(food);
                Business business=new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setRemarks(rs.getString("remarks"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                c.setBusiness(business);
                list.add(c);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            DBUtil.close(rs,prep);
        }

    }

    @Override
    public int saveCart(Cart cart) throws Exception {
        try {
            int result=0;
            conn=DBUtil.getConnection();
            String sql="insert into cart values (null,?,?,?,1)";
            prep=conn.prepareStatement(sql);
            prep.setInt(1,cart.getFoodId());
            prep.setInt(2,cart.getBusinessId());
            prep.setString(3,cart.getUserId());
            result=prep.executeUpdate();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            DBUtil.close(prep);
        }

    }

    @Override
    public int removeCart(Cart cart) throws Exception {
        try {
            int result=0;
            conn=DBUtil.getConnection();
            StringBuilder sb=new StringBuilder("delete from cart where 1=1");
            List params=new ArrayList();
            if (cart.getFoodId()!=null){
                sb.append(" and foodId=?");
                params.add(cart.getFoodId());
            }
            if (cart.getBusinessId()!=null){
                sb.append(" and businessId=?");
                params.add(cart.getBusinessId());
            }
            if (cart.getUserId()!=null){
                sb.append(" and userId=?");
                params.add(cart.getUserId());
            }
            System.out.println(sb.toString());
            prep=conn.prepareStatement(sb.toString());
            for (int i = 0; i < params.size(); i++) {
                prep.setObject(i+1,params.get(i));
            }
            result=prep.executeUpdate();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            DBUtil.close(prep);
        }

    }

    @Override
    public int updateCart(Cart cart) throws Exception {
        try {
            int result=0;
            conn=DBUtil.getConnection();
            StringBuilder sb=new StringBuilder("update cart set quantity=? where 1=1");
            List params=new ArrayList();
            if (cart.getQuantity()!=null){
                params.add(cart.getQuantity());
            }
            if (cart.getFoodId()!=null){
                sb.append(" and foodId=?");
                params.add(cart.getFoodId());
            }
            if (cart.getBusinessId()!=null){
                sb.append(" and businessId=?");
                params.add(cart.getBusinessId());
            }
            if (cart.getUserId()!=null){
                sb.append(" and userId=?");
                params.add(cart.getUserId());
            }
            System.out.println(sb.toString());
            prep=conn.prepareStatement(sb.toString());
            for (int i = 0; i < params.size(); i++) {
                prep.setObject(i+1,params.get(i));
            }
            result=prep.executeUpdate();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            DBUtil.close(prep);
        }
    }
}
